package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author BaiZY on 2020/8/25.
 * @version 1.0
 */
@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    /**
     * 根据商品分类id，查询规格组的集合
     * @param cid
     * @return
     */
    @RequestMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategoryId(@RequestParam("id") Long cid){
        List<SpecGroupDTO> list = specGroupService.queryGroupByCategoryId(cid);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据条件查询规格参数集合
     * @param cid
     * @param gid
     * @param searching
     * @return
     */
    @RequestMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> queryParamsByCondition(
            @RequestParam(value = "categoryId",required = false) Long cid,
            @RequestParam(value = "groupId",required = false) Long gid,
            @RequestParam(value = "searching",required = false)Boolean searching){

        List<SpecParamDTO> list = specParamService.queryParamsByCondition(cid,gid,searching);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param cid
     * @return
     */
    @RequestMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupAndParamByCategoryId(@RequestParam("id") Long cid){
        List<SpecGroupDTO> list = specGroupService.queryGroupAndParamByCategoryId(cid);
        return ResponseEntity.ok(list);
    }

    /**
     * 新增规格组
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDTO){
        //新增规格组
        specGroupService.save(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.updateById(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.save(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.updateById(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
